package StudyCode.summary.regularexpressionsmatch;

import java.text.MessageFormat;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class TestHarnessForRegex {
	public static void main(String[] args) {
		Scanner console = new Scanner(System.in);
		while (true) {
			System.out.println("输入正则表达式: ");
			Pattern pattern = Pattern.compile(console.nextLine());
			System.out.println("输入要匹配的字符串并回车: ");
			Matcher matcher = pattern.matcher(console.nextLine());

			boolean found = false;
			while (matcher.find()) {
				System.out.println(MessageFormat.format("匹配到" + " \"%s\" 开始 " + "索引 %d 和结束索引 %d.%n",
						matcher.group(), matcher.start(), matcher.end()));
				found = true;
			}
			if (!found) {
				System.out.println("未匹配到");
			}
		}
	}
}
